package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.quality.entity.SoInQualityOrderLine;
import com.mxpio.erp.common.quality.entity.VEInspectionBillAssembleBadBack;
import com.mxpio.erp.common.quality.entity.VEInspectionBillAssemblyProblem;
import com.mxpio.erp.common.quality.entity.VEInspectionBillPassRate;
import com.mxpio.erp.common.quality.service.SoInQualityOrderLineService;
import com.mxpio.erp.common.quality.service.VEInspectionBillAssemblyProblemService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.entity.Dept;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class VEInspectionBillAssemblyProblemServiceImpl  implements VEInspectionBillAssemblyProblemService {
    @Override
    @Transactional
    public List<VEInspectionBillAssemblyProblem> getTOP10(Criteria criteria) {
        Linq linq =JpaUtil.linq(VEInspectionBillAssemblyProblem.class);
        List<VEInspectionBillAssemblyProblem> list = linq.aliasToBean(VEInspectionBillAssemblyProblem.class).select("itemCode","itemName","drawingNo",
                        linq.criteriaBuilder().sum(linq.root().get("numQuestions")).alias("numQuestions"))
                .where(criteria)
                .groupBy("itemCode","itemName","drawingNo")
                .list();

//        List<VEInspectionBillAssemblyProblem> top10 = list.stream()
//                .sorted(Comparator.comparingInt(VEInspectionBillAssemblyProblem::getNumQuestions).reversed())
//                .limit(10)
//                .collect(Collectors.toList());

        List<VEInspectionBillAssemblyProblem> top10 = list.stream()
                .sorted(Comparator.comparing(VEInspectionBillAssemblyProblem::getNumQuestions, Comparator.reverseOrder()))
                .limit(10)
                .collect(Collectors.toList());

        return top10;
    }
//    @Override
//    @Transactional(readOnly = true)
//    public List<Map<String, Object>> getTOP10(Criteria criteria) {
//        String sql ="SELECT year_month_,\n" +
//                "num_questions_,\n" +
//                "item_name_,\n" +
//                "drawing_no_\n" +
//                "FROM V_MB_ERP_INSPECTION_BILL_ASSEMBLY_PROBLEM  \n" +
//                "ORDER BY NUM_QUESTIONS_ DESC  \n" +
//                "LIMIT 10;";
//        List<Object[]> resultList = JpaUtil.nativeQuery(sql).getResultList();
//        if (resultList == null){
//            List<Map<String, Object>> list = new ArrayList<>();
//            return list;
//        }
//        List<Map<String, Object>> arrayList = new ArrayList<>();// 存map中的数据
//        for (Object[] row : resultList) {
//            Map<String, Object> map = new HashMap<>();// 将数据转成键值格式对存入map
//            String yearMonth = (String) row[0];
//            String numQuestions = row[1].toString();
//            String itemName = (String) row[2];
//            String drawingNo = (String) row[3];
//            map.put("yearMonth", yearMonth);
//            map.put("numQuestions", numQuestions);
//            map.put("itemName", itemName);
//            map.put("drawingNo", drawingNo);
//            arrayList.add(map);
//        }
//        return arrayList;
//    }
}
